﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Toulr.Models;
using Toulr.Services;
using Toulr.Util.Ioc;

public partial class Account_RetrievePwd : CommPageBase
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (IsPostBack)
        {
            string email = txtMail.Value.Trim();
            if (string.IsNullOrEmpty(email))
            {
                AddClientErrorMessageBox(null, "邮箱地址不能为空，请重新输入", null);
                this.txtMail.Focus();
                return;
            }
            if (string.IsNullOrEmpty(this.txtVerifyCode.Value))
            {
                AddClientErrorMessageBox(null, "验证码不能为空，请重新输入", null);
                this.txtVerifyCode.Focus();
                return;
            }
            if (!PageHelper.IsCorrectVerifyCode(txtVerifyCode.Value))
            {
                AddClientErrorMessageBox(null, "校验码错误,请重新输入！");
                txtVerifyCode.Focus();
                return;
            }

            IUserContextService iUser = UnityHelper.GetInstance<IUserContextService>();

            if (!(iUser.EmailRegistered(email).Result))
            {
                AddClientErrorMessageBox(null, "您输入的邮箱地址还没有被注册，请检查你输入的Email的地址！<br>您也可以选择注册当前的Email地址。", null);
                this.txtMail.Focus();
                return;
            }

            //todo 调用找回密码方法
            var actionResult = iUser.ForgetPassword(email);

            if (actionResult == null)
            {
                PageHelper.Go2Error(actionResult.Tips);
            }

            if (actionResult.ResultCode > 0)
            {
                AddClientErrorMessageBox(null, "找回密码失败：" + actionResult.Tips, null);
                return;
            }

            if (actionResult.ResultCode == 0)
            {
                AddClientErrorMessageBox(null, "新密码已经发送到您的邮箱，请查收！", null);
            }
        }


    }
    [AjaxPro.AjaxMethod]
    public bool CheckEmailExists(string email)
    {
        IUserContextService iUser = UnityHelper.GetInstance<IUserContextService>();
        return iUser.EmailRegistered(email).Result;
    }
}